<!--
 * @Author: your name
 * @Date: 2020-12-03 15:52:30
 * @LastEditTime: 2021-01-26 19:23:46
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \solaxcloud\src\views\inventory\inventory_push.vue
-->
<template>
    <div>
        <el-dialog
        :title="$t('inventoryManage.push')"
        :visible="newVisibleGroup"
        width="700px"
        @close="()=>{$store.commit('setNewVisibleGroup',false)}"
        :close-on-click-modal='false'
        >   
            <el-row :gutter="20">
                <el-col :span="19" :offset="2">
                    <el-form ref="form" :model="form" label-width="160px" >
                        <el-form-item :label="$t('agent.firmType')" prop="type" :rules="[{ required: true, message: $t()}]">
                            <el-select v-model="form.type" filterable :placeholder="$t('pleaseSelect')"  @change='typeChange'>
                                <el-option
                                    v-for="item in typeList"
                                    :key="item.id"
                                    :label="item.text"
                                    :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item :label="$t('inventoryManage.companyName')" prop="name" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                            <el-select v-model="form.name" filterable :placeholder="$t('pleaseSelect')" clearable >
                                <el-option
                                    v-for="item in nameList"
                                    :key="item.sid"
                                    :label="item.firmName"
                                    :value="item.sid">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item :label="$t('version.versionService.fileURL')" prop="file" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                            <baseFile v-model="form.file" style="width:100%"  >
                            </baseFile>
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>
            <el-row :gutter="20">
                <el-col :span="19" :offset="4">
                    <el-button type="primary" @click="downloadModal('form')">{{$t('download.templates')}}</el-button>
                    <span style="color:red;">{{$t('inventoryManage.pushMsg')}}</span>
                </el-col>
            </el-row>
            <span slot="footer" class="dialog-footer">
                <el-button @click="()=>{$store.commit('setNewVisibleGroup',false)}">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="save('form')">{{$t('sure')}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import baseFile from '../../components/common/baseFileInput'
export default {
    components: {
        baseFile
    },
    props: {
        getUserTypeFlag:{
            type:String,
            default:''
        }
    },
    data() {
        return {
            form:{
                type:'',
                name:"",
                file:''
            },
            nameList:[]
        };
    },
    computed: {
        newVisibleGroup(){
            return this.$store.state.sysStore.newVisibleGroup
        },
        typeList(){
            let obj = [{
                id:'2',
                text:this.$t('inventoryManage.breachStatus')
            },{
                id:'3',
                text:this.$t('agent')
            },{
                id:'4',
                text:this.$t('installer')
            }]

            let obj2 = [{
                id:'3',
                text:this.$t('agent')
            },{
                id:'4',
                text:this.$t('installer')
            }]
            if(this.userType == 2){
                this.form.type = obj[0].id
                return obj
            }
            if(this.userType == 3){
                this.form.type = obj2[0].id
                return obj2
            }
        },
        userType(){
            return localStorage.getItem('userType')
        },
        lan(){
            return localStorage.getItem('lan')
        },
    },
    watch: {

    },
    created() {

    },
    mounted() {
        this.wmsproductinfoGetDestinationFirmList()
    },
    methods: {
        downloadModal(){
            if(this.getUserTypeFlag == '0'){
                // window.open(process.env.VUE_APP_DOWNLOAD_PROXY+'/resource/files/warehouseManagement/deliveryRecordTemplate.xlsx')
                this.comm.windowOpen('/resource/files/warehouseManagement/deliveryRecordTemplate.xlsx')
            }else{
                this.comm.windowOpen('/wmsstock/downloadTemplate')
            }
        },
        async wmsproductinfoDeliverGoods(){
            let res = await this.API.wmsproductinfoDeliverGoods({
                file:this.form.file,
                firmName:this.comm.returnCode(this.nameList,this.form.name,'sid','firmName'),
                type:this.form.type,
                id:this.form.name,
            })
            if(!res.success){
                this.comm.showMsg(false)
            }else{
                this.$store.commit('setNewVisibleGroup',false)
                if(res.object&&(res.object.repeatSnList.length>0||res.object.underStockList.length>0)){
                    if(res.object.underStockList.length>0){
                        for(var i = 0;i<res.object.underStockList.length;i++){
                            res.object.repeatSnList.push(res.object.underStockList[i].sn)
                        }
                    }
                    this.comm.showMsg(false, this.$t('inventoryManage.pushErroy2') + `${JSON.stringify([...res.object.repeatSnList])}`)
                    this.$emit('uploadData',true)
                }else{
                    this.$emit('uploadData',true)
                }
            }
        },
        typeChange(){
            this.wmsproductinfoGetDestinationFirmList()
        },
        async wmsproductinfoGetDestinationFirmList(){
            let res = await this.API.wmsproductinfoGetDestinationFirmList({
                type:this.form.type
            })
            this.nameList = res.object
        },
        save(formName){
            this.$refs[formName].validate((valid) => {
              if (valid) {
                  this.wmsproductinfoDeliverGoods()
              } else {
                console.log('error submit!!');
                return false;
              }
            });
        },
    },
};
</script>

<style  lang="less" scoped>

</style>
